//
//  FKDownloadViewController.m
//  FK
//
//  Created by handy wang on 6/8/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "FKDownloadViewController.h"
#import "FKDownloadManager.h"
#import "FKDownloadingTableViewDataSourceImpl.h"
#import "FKDownloadingTableViewDelegateImpl.h"
#import "FKDownloadedTableViewDataSourceImpl.h"
#import "FKDownloadedTableViewDelegateImpl.h"
#import "FKDownloadTask.h"

@interface FKDownloadViewController ()

- (void)initUI;

- (void)setDownloadListViewMode:(FKDownloadListViewMode)downloadListViewMode;

- (void)loadBatchDownloadTasks;

- (void)didFinishLoadBatchDownloadTasks;

@end


@implementation FKDownloadViewController

#pragma mark - Lifecycle

- (void)loadView {
    [super loadView];
    
    //TODO: Show loading UI
    
    [self initUI];
    
    [NSThread detachNewThreadSelector:@selector(loadBatchDownloadTasks) toTarget:self withObject:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [_downloadingTableView release];
    _downloadingTableView = nil;
    
    [_downloadedTableView release];
    _downloadedTableView = nil;
    
    [super dealloc];
}

#pragma mark - Public methods implementation

- (void)setDownloadListViewMode:(FKDownloadListViewMode)downloadListViewMode {
    switch (downloadListViewMode) {
        case FKDownloadListViewDownloadingMode: {
            _downloadingTableView.hidden = NO;
            _downloadedTableView.hidden = YES;
            break;
        }
        case FKDownloadListViewDownloadedMode: {
            _downloadingTableView.hidden = YES;
            _downloadedTableView.hidden = NO;
            break;
        }
        default:{
            break;
        }
    }
}

#pragma mark - Private methods implementation

- (void)initUI {
    //正在下载列表和已下载列表切换按钮
    if (_downloadListSwitcher) {
        [_downloadListSwitcher release];
        _downloadListSwitcher = nil;
    }
    _downloadListSwitcher = [[FKDownloadListSwitcher alloc] initWithIDelegate:self];
    [self.view addSubview:_downloadListSwitcher];
    
    //正在下载列表
    CGRect _tableViewCGRect = CGRectMake(0, _downloadListSwitcher.frame.size.height, 
                                         _downloadListSwitcher.frame.size.width, 
                                         self.view.frame.size.height-_downloadListSwitcher.frame.size.height);
    if (_downloadingTableView) {
        [_downloadingTableView release];
        _downloadingTableView = nil;
    }
    _downloadingTableView = [[UITableView alloc] initWithFrame:_tableViewCGRect style:UITableViewStylePlain];
    _downloadingTableView.dataSource = [[FKDownloadingTableViewDataSourceImpl alloc] init];
    _downloadingTableView.delegate = [[FKDownloadingTableViewDelegateImpl alloc] init];
    [self.view addSubview:_downloadingTableView];
    
    //已下载列表
    if (_downloadedTableView) {
        [_downloadedTableView release];
        _downloadedTableView = nil;
    }
    _downloadedTableView = [[UITableView alloc] initWithFrame:_tableViewCGRect style:UITableViewStylePlain];
    _downloadedTableView.dataSource = [[FKDownloadedTableViewDataSourceImpl alloc] init];
    _downloadedTableView.delegate = [[FKDownloadedTableViewDelegateImpl alloc] init];;
    [self.view addSubview:_downloadedTableView];
    
    //默认显示正在下载列表
    [self setDownloadListViewMode:FKDownloadListViewDownloadingMode];
}

- (void)loadBatchDownloadTasks {
    
    FKDownloadTask *_t1 = [[FKDownloadTask alloc] init];
    _t1.name = @"一吻天荒";
    _t1.url = @"http://zhangmenshiting2.baidu.com/data2/music/14865822/14865822.mp3?xcode=06d1ba1901e93d939ed7ba15aa467a51&mid=0.29429445754123";
    [[FKDownloadManager sharedInstance] addANewTask:_t1];
    
    FKDownloadTask *_t2 = [[FKDownloadTask alloc] init];
    _t2.name = @"爱别离";
    _t2.url = @"http://zhangmenshiting2.baidu.com/data2/music/14865822/14865822.mp3?xcode=06d1ba1901e93d939ed7ba15aa467a51&mid=0.29429445754123";
    [[FKDownloadManager sharedInstance] addANewTask:_t2];
    
    FKDownloadTask *_t3 = [[FKDownloadTask alloc] init];
    _t3.name = @"第九次恋爱";
    _t3.url = @"http://zhangmenshiting2.baidu.com/data2/music/14859131/14859131.mp3?xcode=06d1ba1901e93d937c370964f995582b&mid=0.49082138473775";
    [[FKDownloadManager sharedInstance] addANewTask:_t3];
    
    FKDownloadTask *_t4 = [[FKDownloadTask alloc] init];
    _t4.name = @"你还认不认得我";
    _t4.url = @"http://zhangmenshiting2.baidu.com/data2/music/14740275/14740275.mp3?xcode=8d9804f0b6f02307aee64b0fbfb4525a&mid=0.75570090454753";
    [[FKDownloadManager sharedInstance] addANewTask:_t4];
    
    
    [self performSelectorOnMainThread:@selector(didFinishLoadBatchDownloadTasks) withObject:nil waitUntilDone:NO];
    [_downloadingTableView reloadData];
    [_downloadedTableView reloadData];    
}

- (void)didFinishLoadBatchDownloadTasks {
    //TODO: Hide loading UI

    [[FKDownloadManager sharedInstance] addDelegate:self];
    [[FKDownloadManager sharedInstance] startDownload];
}

#pragma mark - FKDownloadManagerDelegate

- (void)noTasksToDownload {
    
}

- (void)requestFailed:(ASIHTTPRequest *)request withDownloadTask:(FKDownloadTask *)downloadintTaskParam {
    [_downloadingTableView reloadData];
}

- (void)requestFinished:(ASIHTTPRequest *)request withDownloadTask:(FKDownloadTask *)downloadintTaskParam {
    [_downloadingTableView reloadData];
}

- (void)didFinishAllTasksDownload {
    [_downloadingTableView reloadData];
}

@end